<?php 
	if(isset($_REQUEST['xml']) && ($_REQUEST['xml'] == '1')){
		$xml=1;
	}else{$xml=0;}
	
	try{
		$db = new PDO('sqlite:../../projecto1.db');
	}
	catch(PDOException $err){
		if($xml==0){
			$error['Result'] = array('Register'=>'DatabaseError');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('Register');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('DatabaseError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	$bool=0;
	if(isset($_REQUEST['username']) && ($_REQUEST['username'] != '')){
		$login=$_REQUEST['username'];}
	else{$bool=1;}
	if(isset($_REQUEST['pass']) && ($_REQUEST['pass'] != '')){
		$pass=sha1($_REQUEST['pass']);}
	else{$bool=1;}
	$result1 = $db->query('SELECT * FROM User');
	foreach( $result1 as $row) {
		if($row['userName'] == $login){
			$bool=1;
		}
	}
	if($bool == 0){
		/*$sub = "insert into User values (NULL, \"" . $login . "\",  \"" . $pass . "\")";
		$insert = $db->prepare($sub);
		$insert->execute();*/
		$stmt = $db->prepare('INSERT INTO User values (NULL, :login, :pass)');
		$stmt->bindParam(':login', $login);
		$stmt->bindParam(':pass', $pass);
		$stmt->execute();
		session_start();
		$_SESSION['username'] = $login;
		//header("refresh:2; url=../links/user.php");
		//echo "Login successful. Redirecting in 2 seconds...";
		if($xml==0){
			$error['Result'] = array('Register'=>'Success');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('Register');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('Success');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	else{
		if($xml==0){
			$error['Result'] = array('Register'=>'alreadyExistentUsernameError');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('Register');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('alreadyExistentUsernameError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
?>
